package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.Equipment;
import com.chain.qs.modules.biz.service.EquipmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 设备类型Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/equipment")
public class EquipmentController extends BaseController {

    @Resource
    private EquipmentService equipmentService;


    @ModelAttribute
    public Equipment get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return equipmentService.get(id);
        } else {
            return new Equipment();
        }
    }


    @RequestMapping(value = {"list", ""})
    public String list(Equipment equipment, Model model) {
        model.addAttribute("list", equipmentService.findList(equipment));
        return "modules/biz/equipmentList";
    }

    @RequestMapping(value = "form")
    public String from() {
        return "modules/biz/equipmentForm";
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public String save(Equipment equipment) {
        if (equipment.getId() == null || equipment.getId().equals("")) {//新增
            if (equipmentService.getByKid(equipment) != null) {
                return "kidIsExist";
            }
        }
        equipmentService.save(equipment);
        return "success";
    }


    @RequestMapping(value = "delete")
    public String delete(Equipment equipment, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/equipment/";
        }
        equipmentService.delete(equipment);
        addMessage(redirectAttributes, "删除设备成功");
        return "redirect:" + adminPath + "/biz/equipment/";
    }
}
